package com.efresh.nico.gr_superman;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;

import java.util.List;

/**
 * Created by admin on 2015/11/19.
 */
public class ScanZbra extends Activity {
    private TextView resultTextView;
    private EditText qrStrEditText;
    private ImageView qrImgImageView;
    private AlertDialog.Builder alertDialog ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        setContentView(R.layout.main);

        resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
        qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
        qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
        alertDialog =new AlertDialog.Builder(ScanZbra.this);
        Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
        scanBarCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent openCameraIntent = new Intent(ScanZbra.this,CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
            }
        });
        final Button completeOrder = (Button) findViewById(R.id.completeOrder);
        completeOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.setTitle("提醒");
                alertDialog.setCancelable(false);
                alertDialog.setMessage("确定强制完成订单吗？");
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String orderId = "" ;
                        if(resultTextView.getText().toString().isEmpty()){
                            if(qrStrEditText.getText().toString().isEmpty()){
                                Toast.makeText(ScanZbra.this,"请输入订单号",Toast.LENGTH_SHORT).show();
                            }else{
                                orderId = qrStrEditText.getText().toString() ;
                            }
                        }else {
                            orderId = resultTextView.getText().toString() ;
                        }
                        completeOrder(orderId);
                    }
                });
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.show();

            }
        });
        Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);
        generateQRCodeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    String contentString = qrStrEditText.getText().toString();
                    if (!contentString.equals("")) {

                        Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);
                        qrImgImageView.setImageBitmap(qrCodeBitmap);
                    }else {
                        Toast.makeText(ScanZbra.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
                    }

                } catch (WriterException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    private void completeOrder(String orderId) {
        AVQuery<AVObject> query = new AVQuery<AVObject>("Order");
        query.whereEqualTo("orderid",orderId);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(list.size()>0){
                    for (AVObject order:list){
                        order.put("status",9000);
                        order.saveInBackground();
                    }
                    Toast.makeText(ScanZbra.this,"强制完成订单成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(ScanZbra.this,"订单号不存在",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {

            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            resultTextView.setText(scanResult);

        }
    }
}
